# 3、将爬虫脚本优化成多线程。  https://tv.2345.com/top/

import requests
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor

cookies = {
    'YSRF': 'own',
    'uUiD': '223061734673203068927',
    'Hm_lvt_9bb494db4665e40147cd57b89d6628e2': '1734673204',
    'Hm_lpvt_9bb494db4665e40147cd57b89d6628e2': '1734673204',
    'HMACCOUNT': 'B2C21EEE3F1F23B1',
}

headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Cache-Control': 'max-age=0',
    'Connection': 'keep-alive',
    'Sec-Fetch-Dest': 'document',
    'Sec-Fetch-Mode': 'navigate',
    'Sec-Fetch-Site': 'none',
    'Sec-Fetch-User': '?1',
    'Upgrade-Insecure-Requests': '1',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
}

def extract_title(tag):
    try:
        return tag.get('title', '')
    except Exception as e:
        print(f"Error extracting title from tag: {e}")
        return ''


if __name__ == '__main__':
    response = requests.get('https://tv.2345.com/top/', cookies=cookies, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    data = soup.find_all('a', class_='aPlayBtn')
    with ThreadPoolExecutor(max_workers=5) as executor:
        titles = list(executor.map(extract_title, data))

    with open('movie_names3.txt', 'a', encoding='utf8') as f:
        for title in titles:
            if title:
                f.write(title + '\n')
